package com.cv.system.dal.mapper.logger;

import com.cv.framework.common.exception.constants.GlobalErrorCodeConstants;
import com.cv.framework.common.pojo.PageResult;
import com.cv.framework.mybatis.core.mapper.BaseMapperX;
import com.cv.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cv.system.controller.system.logger.vo.operateLog.OperateLogPageReqVO;
import com.cv.system.dal.dataobject.logger.SystemOperateLogDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;

/**
 * 系统操作日志数据层
 *
 * @author Charles_XDXD
 */
@Mapper
public interface SystemOperateLogMapper extends BaseMapperX<SystemOperateLogDO> {

    default PageResult<SystemOperateLogDO> selectPage(OperateLogPageReqVO reqVO, Collection<Long> userIds) {
        LambdaQueryWrapperX<SystemOperateLogDO> wrapper = new LambdaQueryWrapperX<SystemOperateLogDO>()
                .likeIfPresent(SystemOperateLogDO::getModule, reqVO.getModule())
                .inIfPresent(SystemOperateLogDO::getUserId, userIds)
                .eqIfPresent(SystemOperateLogDO::getType, reqVO.getType())
                .betweenIfPresent(SystemOperateLogDO::getStartTime, reqVO.getStartTime());

        if (Boolean.TRUE.equals(reqVO.getSuccess())) {
            wrapper.eq(SystemOperateLogDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        } else if (Boolean.FALSE.equals(reqVO.getSuccess())) {
            wrapper.gt(SystemOperateLogDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        }

        wrapper.orderByDesc(SystemOperateLogDO::getId);
        return selectPage(reqVO, wrapper);
    }

}
